package com.example.address.service;

import com.example.address.DAO.ResultAddressMapper;
import com.example.address.DTO.LocationResultAddress;
import com.example.address.entity.LocationAddress;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class AddressGetter {

    private LocationGetter getLocation;

    private ResultAddressMapper resultAddressMapper;

    @Transactional
    private int setPoint(Integer id, Double longitude, Double latitude) {
        return resultAddressMapper.setPoint(id, longitude, latitude);
    }

    public LocationResultAddress
    getResultAddressById(Integer id) {
        LocationAddress locationAddress = resultAddressMapper.selectAddressById(id);

        String text = String.join(", ", locationAddress.getDetail()
                , locationAddress.getRegion(), locationAddress.getCity());

        List<Double> point = getLocation.getPoint(text);

        return new LocationResultAddress(point.get(0), point.get(1), text);
    }

    public LocationResultAddress
    getCacheResultAddressById(Integer id) {
        LocationAddress locationAddress = resultAddressMapper.selectFullAddressById(id);

        String text = String.join(", ", locationAddress.getDetail()
                , locationAddress.getRegion(), locationAddress.getCity());

        if (locationAddress.getLongitude() == null) {
            List<Double> point = getLocation.getPoint(text);
            locationAddress.setLongitude(point.get(0));
            locationAddress.setLatitude(point.get(1));
            resultAddressMapper.setPoint(
                    id,
                    locationAddress.getLongitude(),
                    locationAddress.getLatitude()
            );
        }

        return new LocationResultAddress(locationAddress.getLongitude(),
                locationAddress.getLatitude(),
                text);
    }

    public List<LocationResultAddress>
    getResultAddressByBill(String bill) {
        List<LocationAddress> locationAddresses =
                resultAddressMapper.selectAddressByBill(bill);
        List<LocationResultAddress> res = new ArrayList<>();
        for (LocationAddress locationAddress : locationAddresses) {
            String text = String.join(", ", locationAddress.getDetail()
                    , locationAddress.getRegion(), locationAddress.getCity());
            List<Double> point = getLocation.getPoint(text);
            res.add(new LocationResultAddress(point.get(0), point.get(1), text));
        }
        return res;
    }

    public List<LocationResultAddress>
    getCacheResultAddressByBill(String bill) {
        List<LocationAddress> locationAddresses =
                resultAddressMapper.selectFullAddressByBill(bill);
        List<LocationResultAddress> res = new ArrayList<>();
        for (LocationAddress locationAddress : locationAddresses) {
            String text = String.join(", ", locationAddress.getDetail()
                    , locationAddress.getRegion(), locationAddress.getCity());
            if (locationAddress.getLongitude() == null) {
                List<Double> point = getLocation.getPoint(text);
                locationAddress.setPoint(point.get(0), point.get(1));
                resultAddressMapper.setPoint(
                        locationAddress.getId(),
                        locationAddress.getLongitude(),
                        locationAddress.getLatitude()
                );
            }

            res.add(new LocationResultAddress(
                    locationAddress.getLongitude(),
                    locationAddress.getLatitude(),
                    text)
            );
        }
        return res;
    }

}
